#!/usr/bin/python
import os, sys, string, random
from openeye.oechem import *

#input: mol2 file with hyrdrogens from moloc
#cp atom names from output-mol2 file to output-pdb file

in_file = oemolistream(sys.argv[1])

out_file_pdb = oemolostream(sys.argv[2])

mol = OEGraphMol()

dict = {}

counter = 1

OEReadMolecule(in_file, mol)
in_file.close()

OEWritePDBFile(out_file_pdb, mol)
#OEPDBOrderAtoms(mol)
out_file_pdb.close()

gen_pdb = open(sys.argv[2], 'r')
output = []
for line in gen_pdb.readlines():
   if line[0:4] == 'ATOM':
	symbol = line[13:14]
	symbol = symbol.strip()
	if dict.has_key(symbol):
		dict[symbol] = dict[symbol] + 1
	else:
		dict[symbol] = 1
	name = symbol + str(dict[symbol])
	

	new_line = line[0:12] + name 
	for space in range(0,5-len(name)):
		new_line = new_line + ' '
	new_line = new_line + line[17:54] + '\n'
	output.append(new_line)

gen_pdb.close()
out_file = open(sys.argv[2], 'w')
for line in output:
	out_file.write(line)
out_file.close()

	





#pdb_file = oemolistream('test.pmdb')

out_file_mol2 = oemolostream(sys.argv[3])

atom_list = []
#OEReadMolecule(pdb_file, mol)
for atom in mol.GetAtoms():
	name = atom.GetName()
	#print name
	
	atom.SetName('C3')
	#print atom.GetName()
	atom_list.append(atom)

mol.OrderAtoms(atom_list)
for atom in mol.GetAtoms():
	#print atom.GetName()
	a = 1



OEWriteMolecule(out_file_mol2, mol)

print "dont' forget to copy the new pdb file into your rec.amb"
	

	


